package com.example.android01.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.android01.MainActivity;

/**
 * 基类
 */
public class BaseActivity extends AppCompatActivity {

    public Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
    }

    /**
     * Toast 提示封装(只能在主线程使用)
     * @param msg
     */
    public void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();

    }

    /**
     * Toast 提示封装（异步）
     * @param msg
     */
    public void showToastSync(String msg){
        Looper.prepare();
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
        Looper.loop();

    }

    /**
     * 跳转
     * new Intent(MainActivity.this, LoginActivity.class) 第一个参数：当前页面；参数二：跳转页面
     * @param cls
     */
    public void navigateTo(Class cls){
        Intent in = new Intent(context, cls);
        startActivity(in);
    }


    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(context, cls);
        in.setFlags(flags);
        startActivity(in);
    }

    /**
     * 本地缓存
     * @param key
     * @param val
     */
    protected void insertVal(String key, String val) {
        SharedPreferences sp = getSharedPreferences("sp_ttit", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.commit();
    }
}
